/* var_pool.cpp */

#include "var_pool.h"
#include "mm5_file.h"
#include <algorithm>

namespace dodomet
{
namespace mm5
{

void Var_Pool::add_mm5_file(const MM5_File& mm5_file)
{
	for(MM5_File::const_iterator iter = mm5_file.begin(); iter != mm5_file.end(); ++iter)
	{
		iterator local_iter;
		for(local_iter = vars_.begin();
			local_iter != vars_.end();
			++local_iter)
		{
			if(iter->name() == local_iter->name())
				break;
		}

		if(local_iter == vars_.end())
		{
			vars_.push_back(*iter);
		}
		else
		{
			local_iter->merge(*iter);
		}
	}
}

Var_Pool::iterator Var_Pool::begin()
{
	return vars_.begin();
}

Var_Pool::iterator Var_Pool::end()
{
	return vars_.end();
}

Var_Pool::const_iterator Var_Pool::begin() const
{
	return vars_.begin();
}

Var_Pool::const_iterator Var_Pool::end() const
{
	return vars_.end();
}

int Var_Pool::var_num() const
{
	return vars_.size();
}

Var_Pool::iterator Var_Pool::find_var_name(const std::string& var_name)
{
	iterator iter;
	for(iter = begin(); iter != end(); ++iter)
	{
		if(iter->name() == var_name)
			break;
	}
	return iter;
}

Var_Pool::const_iterator Var_Pool::find_var_name(const std::string& var_name) const
{
	const_iterator iter;
	for(iter = begin(); iter != end(); ++iter)
	{
		if(iter->name() == var_name)
			break;
	}
	return iter;
}

} // namespace dodomet::mm5
} // namespace dodomet
